import datetime
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

#元素定位

#1. 打开浏览器---驱动管理
ChromIns = ChromeDriverManager().install()
##创建谷歌浏览器驱动对象
driver = webdriver.Chrome(service = Service(ChromIns))

driver.get("https://www.baidu.com/")
#driver.get("https://fanyi.baidu.com/mtpe-individual/multimodal")
#time.sleep(2)

# ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)  #获取每个元素对应的文本信息

#点击事件
#查找元素+点击元素
#driver.find_element(By.CSS_SELECTOR, "#su").click()
# ele = driver.find_element(By.CSS_SELECTOR, "#su")
# ele.click()

# driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(3) > a > span.title-content-title").click()

# #查找元素 + 输入文本
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("C++官方文档")
# ele = driver.find_element((By.CSS_SELECTOR, "#kw"))
# ele.send_keys("C++官方文档")

# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("C++官方文档")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#kw").clear()
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("Java官方文档")
# time.sleep(2)

# #获取文本信息
# text = driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(5) > a > span.title-content-title").text
# print(text)
# assert text =="武契奇排除万难抵俄后在红场发声"

# #获取百度一下按钮上的文本
# #text = driver.find_element(By.CSS_SELECTOR, "#su").text  #这里获取不到对应的"百度一下"属性值
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text)
# assert text == "百度一下"

# title = driver.title
# url = driver.current_url
# print(title)
# print(url)



#点击百度首页的图片功能，跳转到百度图片页面  --error
# print("before" + driver.title)
# print("before" + driver.current_url)
#
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
#
# time.sleep(3)
# print("after" + driver.title)
# print("after" + driver.current_url)


# #点击百度首页的图片功能，跳转到百度图片页面
# print("before:" + driver.title)
# print("before:" + driver.current_url)
#
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# #获取当前页面的句柄 -- 第一个标签页
# curHandle = driver.current_window_handle
# #获取所有句柄
# allHandle = driver.window_handles
# #遍历所有的句柄，切换到新的页面
# for handle in allHandle:
#     if handle != curHandle:
#         # 切换句柄
#         #driver.switch_to_window(handle) //旧的写法，已经被淘汰
#         driver.switch_to.window(handle)
#
# #测试跳转结果
# print("after:" + driver.title)
# print("after:" + driver.current_url)



# print("before:" + driver.title) #百度翻译文本翻译的标题
# print("before:" + driver.current_url) #百百度翻译文本翻译的url
# driver.find_element(By.CSS_SELECTOR, "#root > div:nth-child(2) > div > div.QWmdE1v6 > div > div:nth-child(2) > span").click()
# time.sleep(2)
# print("after:" + driver.title) # 百度翻译文档翻译标题
# print("after:" + driver.current_url) #百度翻译文档翻译的url


# # 设置窗口大小
# #最大化
# time.sleep(2)
# driver.maximize_window()
# #最小化
# time.sleep(2)
# driver.minimize_window()
# #窗口全屏
# time.sleep(2)
# driver.fullscreen_window()
# #手动设置尺寸
# time.sleep(2)
# driver.set_window_size(1024, 800)


#添加屏幕截图
# filename = "./images/test.png"
# driver.save_screenshot(filename)

# print(datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S'))
# filename = "auototest-" + datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '.png'
# driver.save_screenshot("./images/" + filename)

driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
time.sleep(2)
driver.close()

time.sleep(3)


driver.quit()













